package com.ptl.bp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ptl.bp.config.ApiResult;
import com.ptl.bp.entity.BatteryPassport;
import com.ptl.bp.dto.BatteryPassportQueryDTO;
import com.ptl.bp.dto.CountDTO;
import com.ptl.bp.dto.IdDTO;
import com.ptl.bp.service.BatteryPassportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/battery-passports")
@RequiredArgsConstructor
@Tag(name = "电池护照管理API")
public class BatteryPassportController {

    private final BatteryPassportService batteryPassportService;

    @PostMapping
    @Operation(summary = "创建电池护照")
    public ApiResult<Long> create(@RequestBody BatteryPassport passport) {
        return ApiResult.success(batteryPassportService.createPassport(passport));
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取电池护照详情")
    public ApiResult<BatteryPassport> getById(
            @Parameter(description = "电池护照ID") @PathVariable String id) {
        return ApiResult.success(batteryPassportService.getById(id));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新电池护照")
    public ApiResult<Boolean> update(
            @Parameter(description = "电池护照ID") @PathVariable Long id,
            @RequestBody @Valid BatteryPassport passport) {
        passport.setId(id);
        return ApiResult.success(batteryPassportService.updateById(passport));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除电池护照")
    public ApiResult<Boolean> delete(
            @Parameter(description = "电池护照ID") @PathVariable String id) {
        return ApiResult.success(batteryPassportService.removeById(id));
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询电池护照")
    public ApiResult<IPage<BatteryPassport>> pageQuery(
            @Parameter(description = "分页参数") Page<BatteryPassport> page,
            @Parameter(description = "查询条件") BatteryPassportQueryDTO queryDTO) {
        return ApiResult.success(batteryPassportService.pageQuery(page, queryDTO));
    }

    @Operation(summary = "发布电池护照")
    @PostMapping("/publish")
    public ApiResult<String> publish(@RequestBody @Valid IdDTO dto) {
        batteryPassportService.lambdaUpdate()
                .eq(BatteryPassport::getId, dto.getId())
                .set(BatteryPassport::getDocumentStatus, BatteryPassport.PassportDocumentStatus.PUBLISHED)
                .update();
        return ApiResult.success("操作成功");
    }

    @GetMapping("/export")
    public void exportBatteryPassports(HttpServletResponse response) throws IOException {
    }

    @Operation(summary = "统计已发布,未发布电池")
    @GetMapping("/countByPublishStatus")
    public ApiResult<List<CountDTO>> countByPublishStatus() {
        return ApiResult.success(batteryPassportService.countByPublishStatus());
    }

    @Operation(summary = "月度电池护照生成统计")
    @GetMapping("/countByMonth")
    public ApiResult<List<CountDTO>> countByMonth() {
        return ApiResult.success(batteryPassportService.countByMonth());
    }

    @Operation(summary = "近10年,年度统计")
    @GetMapping("/countByYear")
    public ApiResult<List<CountDTO>> countByYear() {
        return ApiResult.success(batteryPassportService.countByYear());
    }

    @Operation(summary = "电池型号")
    @GetMapping("/countByModel")
    public ApiResult<List<CountDTO>> countByModel() {
        return ApiResult.success(batteryPassportService.countByModel());
    }

    @Operation(summary = "制造商")
    @GetMapping("/countByManufacturer")
    public ApiResult<List<CountDTO>> countByManufacturer() {
        return ApiResult.success(batteryPassportService.countByManufacturer());
    }
}